<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of adminController
 *
 * @author sammerelaraby
 */
class adminController extends Zend_Controller_Action {

    //put your code here
    public function indexAction() {

    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        $login = $auth->getStorage()->read();
        if (!$auth->hasIdentity()) {
            $this->_helper->redirector('index', 'auth');
        } else {
            if ($login->role != 'administrator') {
                $this->_helper->redirector('userpage', 'user');
            } else {

                parent::preDispatch();
            }
        }
    }

    public function showcustomAction() {
        $users = new Zend_Db_Table('users');
        $usersQuery = $users->fetchAll();
        $this->view->allusers = $usersQuery;
    }

    public function editprodsAction() {

    }

    public function editprodAction() {
        $products = new Zend_Db_Table('products');
        $productsQuery = $products->fetchAll();
        $this->view->allproducts = $productsQuery;

        $prodform = new APP_Form_ProductForm();
        $prodform->getElement('prodimg')->setRequired(false);
        $prodName = $this->getRequest()->getParam('data');

        if (strlen($prodName) > 0) {
            $productsRowQuery = $products->fetchRow("product_name='" . $prodName . "'");

            $cats = new Zend_Db_Table('category');
            $catQuery = $cats->fetchRow("category_id='" . $productsRowQuery['cat_id'] . "'");
            $cat = $catQuery['category_name'];

            $prodform->setDefault('prodname', $productsRowQuery['product_name']);
            $prodform->setDefault('prodprice', $productsRowQuery['price']);
            $prodform->setDefault('prodquan', $productsRowQuery['quantity']);
            $prodform->setDefault('prodimg', $productsRowQuery['image']);
            $prodform->setDefault('catselect', $cat);

            $this->view->form = $prodform;


            if ($this->getRequest()->isPost()) {
                if ($prodform->isValid($_POST)) {

                    $catName = $prodform->getElement('catselect')->getValue();
                    $catId = $cats->fetchRow("category_name='" . $catName[0] . "'");

                    $updateProduct = new Zend_Db_Table('products');
                    $img = $prodform->getElement('prodimg')->getValue();
                    if (strlen($img) > 0) {
                        $updateProduct->update(array('product_name' => $_POST['prodname'],
                            'price' => $_POST['prodprice'],
                            'quantity' => $_POST['prodquan'],
                            'image' => $prodform->getElement('prodimg')->getValue(),
                            'cat_id' => $catId['category_id']),
                                "product_id=" . $productsRowQuery['product_id']);
                    } else {
                        $updateProduct->update(array('product_name' => $_POST['prodname'],
                            'price' => $_POST['prodprice'],
                            'quantity' => $_POST['prodquan'],
                            'cat_id' => $catId['category_id']),
                                "product_id=" . $productsRowQuery['product_id']);
                    }
                }
            }
        }
    }

    public function addprodAction() {
        $prodform = new APP_Form_ProductForm();
        $this->view->prodform = $prodform;
        if ($this->getRequest()->isPost()) {
            if ($prodform->isValid($_POST)) {
                $catObject = new Zend_Db_Table('category');
                $catName = $prodform->getElement('catselect')->getValue();
                $catId = $catObject->fetchRow("category_name='" . $catName[0] . "'");

                $products = new Zend_Db_Table('products');
                $new = $products->createRow();
                $new->product_name = $_POST['prodname'];
                $new->price = $_POST['prodprice'];
                $new->quantity = $_POST['prodquan'];
                $new->image = $prodform->getElement('prodimg')->getValue();
                $new->cat_id = $catId['category_id'];
                $new->save();
            }
        }
    }

    public function delprodAction() {
        $products = new Zend_Db_Table('products');
        $productsQuery = $products->fetchAll();
        $this->view->allproducts = $productsQuery;
    }

    ////////////////////////////////Ajax Actions////////////////////////////////////
    public function getcustomAction() {
        $user = $this->_request->getParam("data");
        $users = new Zend_Db_Table('users');
        $info = $users->fetchRow('user_name = "' . $user . '"');
        $this->view->userinfo = $info;
        $this->_helper->layout()->disableLayout();
    }

    public function removeprodAction() {
        $prod = $this->_request->getParam("data");

        $productTable = new Zend_Db_Table('products');
        $productTable->delete('product_name = "' . $prod . '"');
        $products = new Zend_Db_Table('products');
        $productsQuery = $products->fetchAll();

        $this->view->allproducts = $productsQuery;
        $this->_helper->layout()->disableLayout();
    }

    public function sendmailAction() {
        require_once('Zend/Mail.php');
        require_once('Zend/Mail/Transport/Smtp.php');
        $config = array('auth' => 'login',
            'username' => 'sammerel.araby@gmail.com',
            'password' => '13101988-faith',
            'port' => 465,
            'ssl' => 'ssl');


        $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
        Zend_Mail::setDefaultTransport($transport);

        $mail = new Zend_Mail();
        $mail->setBodyText('Hello Sammer It is me zend mail :)');
        $mail->setFrom('sammerel.araby@gmail.com', 'Zend_Mail');
        $mail->addTo('sammerel.araby@gmail.com', 'Sammer');
        $mail->setSubject('Zend_Mail_Test');
        $mail->send();
    }

}

?>
